
<template>    
    <div class="app-container">
        <el-row :gutter="10" class="mb8">            
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                    v-hasPermi="['basic/codeRule/add']"
                    v-show="!disabled"
                >新增</el-button>
            </el-col>            
            <el-col :span="1.5">
                <el-button
                    type="success"
                    icon="el-icon-edit"
                    size="mini"
                    :disabled="single"
                    @click="handleUpdate"
                    v-hasPermi="['basic/codeRule/edit']"
                    v-show="!disabled"
                >修改</el-button>
            </el-col>            
            <el-col :span="1.5">
                <el-button
                    type="danger"
                    icon="el-icon-delete"
                    size="mini"
                    :disabled="multiple"
                    @click="handleDelete"
                    v-hasPermi="['basic/codeRule/delete']"
                    v-show="!disabled"
                >删除</el-button>
            </el-col>
        </el-row>
        <el-table v-loading="loading" :data="codeRuleList"
            @selection-change="handleSelectionChange"            
        >            
            <el-table-column type="selection" width="55" align="center" />            
            <el-table-column label="类型" align="center" prop="key" />            
            <el-table-column label="名称" align="center" prop="name" />            
            <el-table-column label="前缀" align="center" prop="prefix" />            
            <el-table-column label="时间格式" align="center" prop="time" />            
            <el-table-column label="流水号起点" align="center" prop="serialBegin" />            
            <el-table-column label="流水号长度" align="center" prop="serialLength" />            
            <el-table-column label="当前流水号" align="center" prop="serialCounter" />            
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">                    
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['basic/codeRule/edit']"
                            v-show="!disabled"
                    >修改</el-button>                    
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['basic/codeRule/delete']"
                            v-show="!disabled"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>        
        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false">            
            <editor ref="editor"                
            />            
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="confirm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>        
    </div>
</template>
<script>
import {
    listCodeRule,
    delCodeRule,    
} from "@/api/basic/codeRule";
export default {
    name: "CodeRule",
    components:{        
        editor:()=>import('../editor.vue'),        
    },
    props: [        
        "disabled" // 禁用列表页面的增删改按钮
    ],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 总条数
            total: 0,
            // 编码规则表格数据
            codeRuleList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,            
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,                
            },            
        };
    },
    created() {
        this.getList();        
    },
    watch:{        
    },
    methods: {        
        /** 查询编码规则列表 */
        getList() {
            this.loading = true;            
            listCodeRule(this.queryParams).then(response => {                
                this.codeRuleList = response.data.list || [];
                this.total = response.data.total;                
                this.loading = false;
            });
        },        
        // 取消按钮
        cancel() {
            this.open = false;
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length!=1
            this.multiple = !selection.length
        },        
        /** 新增按钮操作 */
        handleAdd() {            
            this.title = "添加编码规则";
            this.open = true;
            this.$nextTick(() => {
                this.$refs.editor.reset();
            })            
        },        
        /** 修改按钮操作 */
        handleUpdate(row) {
            const id = row.id || this.ids
            this.title = "修改编码规则";
            this.open = true;
            this.$nextTick(()=>{
                this.$refs.editor.load(id, ()=>{})
            })
        },        
        /** 提交按钮 */
        confirm: function() {            
            this.$refs.editor.submitForm(()=>{
                this.open = false;
                this.getList();
            })            
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$confirm('是否确认删除编码规则编号为"' + ids + '"的数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function() {
                return delCodeRule(ids);
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            }).catch(function() {});
        },        
    }
};
</script>
